<?php


namespace app\shop\model;

use app\shop\model\MoneyModel;
use think\Model;

class UserModel extends Model
{
    protected $pk = 'user_id';
    protected $table = 'dp_user';
    protected $field = true;

    public static function getList($map=[])
    {
        return self::order('user_id desc')->where($map)->paginate(20);
    }

    public static function getOne($user_id)
    {
        return self::where('user_id',$user_id)->find();
    }

    public static function updateMoney($userId, $type, $value , $desc='')
    {
        $userInfo = self::getOne($userId);
        if($type == 1){
            // 余额增加
            $newUserMoney = round(($userInfo['user_money'] + $value), 2);
        }else{
            // 余额减少
            $newUserMoney = round(($userInfo['user_money'] - $value), 2);
            if($newUserMoney < 0){
                return false;
            }
        }
        // 记录余额改变日志
        MoneyModel::store($userId,$type, $value, $desc);
        $data['user_money'] = $newUserMoney;
        self::where('user_id', $userId)->update($data);
    }
}